<template>

    <!-- 添加或修改对话框 -->
    <lam-dialog :title="title" :visible.sync="open" width="680px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" :disabled="formDisabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="字典编码" prop="dictCode">
              <el-input v-model="form.dictCode" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表名" prop="tableName">
              <span slot="label">
                表名<el-tooltip content="数据库表全名，例: sys_user" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.tableName" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="value字段" prop="valueField">
              <span slot="label">
                value字段<el-tooltip content="作为字典value值的字段，例: sys_user表的username或id" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.valueField" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="text字段" prop="textField">
              <span slot="label">
                text字段<el-tooltip content="作为字典text标签的字段，例: sys_user表的name或昵称" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.textField" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="查询条件" prop="conditionSql">
              <span slot="label">
                查询条件<el-tooltip content="可以自定义SQL，变量用{XXX}，若变量类型为数字类型，则变量加上后缀: XXX_NUM" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.conditionSql" type="textarea" autosize clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <lam-dict-select type="radio" v-model="form.status" dictCode="status_type" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input-number v-model="form.sort" clearable controls-position="right" :min="0" style="width: 100%;"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <lam-dict-select type="radio" v-model="form.type" dictCode="config_type" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" autosize placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </lam-dialog>

</template>

<script>
import { EditMixin } from '@/components/mixins/EditMixin';

export default {
  name: "EditTableDict",
  mixins:[EditMixin],
  data() {
    return {
      url: {
        base: '/sys/tableDict'
      },
      // 表单校验
      rules: {
        dictCode: [{ required: true, message: "字典编码不能为空", trigger: "blur" }, { validator: this.codeUniqueValidator, trigger: 'blur'}],
        tableName: [{ required: true, message: "表名不能为空", trigger: "blur" }],
        valueField: [{ required: true, message: "value字段不能为空", trigger: "blur" }],
        textField: [{ required: true, message: "text字段不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        type: [{ required: true, message: "类型不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        dictCode: null,
        tableName: null,
        valueField: null,
        textField: null,
        type: 2, 
        status: "1",
        sort: null,
        remark: null
      };
    },

    codeUniqueValidator(rule, value, callback) {
      let param = this.operType=='update'?{dictCode: value, id: this.form.id}:{dictCode: value };
      this.checkUniqueAsyn(value, callback, 'tableDictCode', param);
    },

    /** 提交按钮 */
    submitForm(){
      this.saveForm(this.url.base);
    }

  }
};
</script>
